package com.map.baidumapdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;

/**
 * Created by Administrator on 2015/4/15.
 * 定位功能Activity
 */
public class LocationActivity extends Activity {
    private BDLocationListener myLocationListener = new MyLocationListener();
    private Button startLocation;
    private LocationClient mLocationClient;
    //定义mapview
    private MapView mapView;
    private BaiduMap mBaiduMap;

    private TextView locationResult;
    private EditText frequence;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.location);
        mapView = (MapView)findViewById(R.id.bmapView);
        mBaiduMap = mapView.getMap();
        mLocationClient = ((LocationApplication)getApplication()).mLocationClient;
        mLocationClient.registerLocationListener(myLocationListener);
        locationResult = (TextView)findViewById(R.id.textView1);
        ((LocationApplication)getApplication()).mLocationResult = locationResult;
        startLocation = (Button)findViewById(R.id.addfence);
        startLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initLocation();
                if(startLocation.getText().equals(getString(R.string.startlocation))){
                    mLocationClient.start();
                    startLocation.setText(getString(R.string.stoplocation));
                }else{
                    mLocationClient.stop();
                    startLocation.setText(getString(R.string.startlocation));
                }
            }
        });
    }

    @Override
    protected void onStop() {
        mLocationClient.stop();
        super.onStop();
    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置定位模式
        option.setCoorType("bd0911");//返回的定位结果是百度经纬度，默认值gcj02
        int span = 5000;
//        try{
//            span = Integer.valueOf(frequence.getText().toString());
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        option.setScanSpan(span);
        option.setIsNeedAddress(true);//返回的定位结果包含地址信息
        option.setNeedDeviceDirect(true);////返回的定位结果包含手机机头的方向
        option.setAddrType("all");
        mLocationClient.setLocOption(option);
    }

    //定位结果返回
    public class MyLocationListener implements BDLocationListener{

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //经纬度构造对象
            LatLng geoPoint = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
            //地点添加
            BitmapDescriptor bitMap = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka);
            //地图覆盖物选型基类
            OverlayOptions options = new MarkerOptions().position(geoPoint).icon(bitMap);
            mBaiduMap.addOverlay(options);

            //定义地图显示中心
            MapStatus mStatus = new MapStatus.Builder().target(geoPoint).build();
            MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mStatus);
            mBaiduMap.setMapStatus(mMapStatusUpdate);

        }
    }
}
